//index.js
//获取应用实例
const common = require('../../utils/util.js')
const app = getApp()

Page({
    data: {
        statusBarHeight: 0,
        nowDate: '',
        shopInfo: null,
        userInfo: null,
        belongRefer: null,
        popImg: '',
        showAdv: true,
        imgSwiper: [],
        newGoods: [],
        hotSaleGoods: [],
        hotClickGoods: [],
        opusList: [],
        serviceList: [],
        monthServiceList: [],
        typeClass: [],
        typeClassList: [],
        zeroBuy: null,
        zeroBuyList: [],
        guessLikeClass: [],
        guessLikeList: [],
        guessType: '',
        nowPage: 0,
        isLoading: false,
        isAll: false,
        crowdInfo: null,
        shopVideo: []
    },
    onLoad: function(options) {
        if (options.userShare) {
            wx.setStorageSync('userShare', options.userShare)
            if (wx.getStorageSync('token')) {
                let data = {
                    belong_refer: options.userShare
                }
                common.post('User/set_belong_refer', data).then(function(res) {}).catch(function(e) {})
            }
        }
        this.setData({
            nowDate: new Date().getTime()
        })
        // wx.setNavigationBarTitle({
        //     title: '美多了' //页面标题为路由参数
        // })
        if (wx.getStorageSync('token')) {
            this.getUserInfo()
            this.getCrowdInfo()
        }
        this.getSystemInfo()
        this.getShopInfo()
        this.getPopImg()
        this.getBannerList()
        this.getIndexData()
        this.getOpusList()
        // this.getNewGoods()
        // this.getHotSaleGoods()
        // this.getHotClickGoods()
        this.getGuessLikeList()
        this.getServiceList()
        this.monthServiceList()
        this.getTypeClass()
        this.getZeroBuy()
        this.getShopVideo()
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        this.setData({
            isLoading: false
        })
    },
    onShow: function() {

    },
    onPullDownRefresh: function() {
        this.setData({
            statusBarHeight: 0,
            nowDate: '',
            userInfo: null,
            belongRefer: null,
            imgSwiper: [],
            newGoods: [],
            hotSaleGoods: [],
            hotClickGoods: [],
            opusList: [],
            serviceList: [],
            monthServiceList: [],
            typeClass: [],
            typeClassList: [],
            zeroBuy: null,
            zeroBuyList: [],
            guessLikeClass: [],
            guessLikeList: [],
            guessType: '',
            nowPage: 0,
            isLoading: false,
            isAll: false,
            crowdInfo: null,
            shopVideo: []
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    onReachBottom: function() {
        this.getGuessLikeList()
    },

    onShareAppMessage: function(res) {
        let userId = wx.getStorageSync('userId')
        if (userId) {
            return {
                title: '美多了',
                path: '/pages/index/index?userShare=' + userId,
                imageUrl: 'https://www.meiboshi.vip/static/images/wx_sm/home/mds-index-share.png'
            }
        }
    },

    getSystemInfo: function() {
        const that = this
        wx.getSystemInfo({
            success(res) {
                that.setData({
                    statusBarHeight: res.statusBarHeight + 5
                })
            }
        })
    },

    getPopImg: function() {
        const that = this
        let data = {
            activity: 785,
            images_id: 15
        }
        common.post('Images/get_img', data).then(function(res) {
            that.setData({
                popImg: res[0]
            })
        }).catch(function(e) {})
    },

    closeAdv: function() {
        this.setData({
            showAdv: false
        })
    },

    getShopInfo: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1
        }
        common.post('Store/get_store_info', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                shopInfo: res
            })
            wx.setNavigationBarTitle({
                title: res.store_name
            })
        }).catch(function(e) {})
    },

    getUserInfo: function() {
        const that = this
        if (wx.getStorageSync('token')) {
            let data = {}
            common.post('User/user_info', data).then(function(res) {
                if (res.code == 204) {
                    return
                } else {
                    that.setData({
                        userInfo: res
                    })
                }
            }).catch(function(e) {})
        }
    },

    getShopVideo: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1,
            page: 0,
            limit: 2
        }
        common.post('Video/video_list', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopVideo: res
                })
            }
        }).catch(function(e) {})
    },

    getBannerList: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1,
            is_online: 1,
            client: 3,
            page: 0,
            limit: 999
        }
        common.post('Slideshow/slideshow_list', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    imgSwiper: res
                })
            }
        }).catch(function(e) {})
    },

    getIndexData: function() {
        const that = this
        let data = {
            client: 3,
            activity: 785
        }
        common.post('Home/index', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    guessLikeClass: res.mdl_class
                })
            }
        }).catch(function(e) {})
    },

    getCrowdInfo: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1,
            stuats: 1,
            paramter: 1
        }
        common.post('Crowd/crowd_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                crowdInfo: res[0]
            })
        }).catch(function(e) {})
    },

    getOpusList: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1,
            type: 3,
            order_by: 'add_time',
            limit: 1,
            page: 0
        }
        common.post('Moments/moment_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                opusList: res
            })
        }).catch(function(e) {})
    },

    getServiceList: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1,
            is_goods: 8,
            is_sale: '1',
            page: 0,
            limit: 4
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    serviceList: []
                })
            } else {
                that.setData({
                    serviceList: res
                })
            }
        }).catch(function(e) {})
    },

    monthServiceList: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1,
            is_goods: 8,
            is_actvity: 1,
            is_sale: '1',
            page: 0,
            limit: 10
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    monthServiceList: []
                })
            } else {
                that.setData({
                    monthServiceList: res
                })
            }
        }).catch(function(e) {})
    },

    getNewGoods: function() {
        const that = this
        let data = {
            activity: 785,
            is_goods: 99,
            order_by: 'add_time',
            asc: 2,
            page: 0,
            limit: 20
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    newGoods: []
                })
            } else {
                that.setData({
                    newGoods: res
                })
            }
        }).catch(function(e) {})
    },

    getHotSaleGoods: function() {
        const that = this
        let data = {
            activity: 785,
            is_goods: 99,
            order_by: 'number',
            asc: 2,
            page: 0,
            limit: 20
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                hotSaleGoods: res
            })
        }).catch(function(e) {})
    },

    getHotClickGoods: function() {
        const that = this
        let data = {
            activity: 785,
            is_goods: 99,
            order_by: 'click',
            asc: 2,
            page: 0,
            limit: 20
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                hotClickGoods: res
            })
        }).catch(function(e) {})
    },

    getGuessLikeList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let guessLikeList = this.data.guessLikeList
        let data = {
            activity: 785,
            page: this.data.nowPage,
            limit: 10,
            class_id: this.data.guessType,
            is_goods: 99
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    guessLikeList: guessLikeList,
                    isAll: true
                })
            } else {
                guessLikeList = guessLikeList.concat(res)
                that.setData({
                    guessLikeList: guessLikeList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    guessClick: function(e) {
        if (this.data.guessType == e.detail) {
            return
        }
        this.setData({
            guessType: e.detail,
            nowPage: 0,
            isAll: false,
            isLoading: false
        })
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let guessLikeList = this.data.guessLikeList
        let data = {
            activity: 785,
            page: this.data.nowPage,
            limit: 10,
            class_id: this.data.guessType,
            is_goods: 99
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                guessLikeList = []
                that.setData({
                    guessLikeList: guessLikeList,
                    isAll: true
                })
            } else {
                guessLikeList = []
                guessLikeList = guessLikeList.concat(res)
                that.setData({
                    guessLikeList: guessLikeList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    getTypeClass: function() {
        const that = this
        let data = {
            activity: 785,
            type: 2
        }
        common.post('Shelf/Shelf', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    typeClass: []
                })
            } else {
                let typeClassList = that.data.typeClassList
                that.setData({
                    typeClass: res
                })
                that.data.typeClass.forEach((item, i) => {
                    let data = {
                        shelf_id: item.id
                    }
                    common.post('Shelf/Shelf_list', data).then(function(res) {
                        if (res.code == 204) {
                            typeClassList[i] = []
                        } else {
                            typeClassList[i] = res
                        }
                        that.setData({
                            typeClassList: typeClassList
                        })
                    }).catch(function(e) {})
                })
            }
        }).catch(function(e) {})
    },

    getZeroBuy: function() {
        const that = this
        let data = {
            activity: 785,
            type: 6
        }
        common.post('Shelf/Shelf', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    zeroBuy: res[0]
                })
                let zeroBuyList = that.data.zeroBuyList
                let data = {
                    activity: 785,
                    shelf_id: res[0].id
                }
                common.post('Activity/get_activity_goods', data).then(function(res) {
                    if (res.code == 204) {
                        zeroBuyList = []
                    } else {
                        zeroBuyList = res
                    }
                    that.setData({
                        zeroBuyList: zeroBuyList
                    })
                }).catch(function(e) {})
            }
        }).catch(function(e) {})
    },

    beVip: function() {
        wx.navigateTo({
            url: '/pages/goods/shopKeeperGifts/shopKeeperGifts'
        })
    }
})